package com.zelin.util;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ZhangXiaoHan
 * @Date: 2020/09/26/19:54
 * @Description:
 */
public class Appliction {
    private static String driver = "";
    private static String url = "";
    private static String user = "";
    private static String password = "";

    static {
        //1.读取配置加载驱动
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver= bundle.getString("db.driver");
        url= bundle.getString("db.url");
        user= bundle.getString("db.user");
        password= bundle.getString("db.password");

        //2.加载驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //3.得到连接
    public static Connection getConn(){
        try {
            return DriverManager.getConnection(url,user,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //4. 关闭资源
    public static void closeAll(Statement ps, ResultSet rs, Connection conn){
        try{
            if(conn != null) conn.close();
            if(rs != null) rs.close();
            if(ps != null) ps.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
